package com.djl.seckilldemo.util;

import com.djl.seckilldemo.pojo.User;
import com.djl.seckilldemo.vo.RespBean;
import com.fasterxml.jackson.databind.ObjectMapper;
import sun.java2d.pipe.OutlineTextRenderer;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

/**
 * @program: seckill-demo
 * @description: 生成用户工具类
 * @author: Mr.dai
 * @create: 2021-10-24 15:01
 **/
public class UserUtil {

        private  static void  createUser(int count) throws Exception{
            List<User> users=new ArrayList<>(count);
            for (int i = 0; i < count; i++) {
                User user=new User();
                user.setId (13000000000L+i);
                user.setNickname("user"+i);
                user.setSalt("1a2b3c4d");
                user.setLoginCount(1);
                user.setRegisterDate(new Date());
                user.setPassword(MD5Util.inputPassToDbPass("123456",user.getSalt()));
                users.add(user);
            }
            System.out.println("create users");
            //Connection conn = getConn();
            //String sql="insert into t_user(login_count,nickname,register_date,salt,password,id) values(?,?,?,?,?,?)";
            //PreparedStatement preparedStatement = conn.prepareStatement(sql);
            //for (int i = 0; i < users.size(); i++) {
            //    User user = users.get(i);
            //    preparedStatement.setInt(1,user.getLoginCount());
            //    preparedStatement.setString(2,user.getNickname());
            //    preparedStatement.setTimestamp(3,new Timestamp(user.getRegisterDate().getTime()));
            //    preparedStatement.setString(4,user.getSalt());
            //    preparedStatement.setString(5,user.getPassword());
            //    preparedStatement.setLong(6,user.getId());
            //    //添加批量
            //    preparedStatement.addBatch();
            //}
            ////执行批量
            //preparedStatement.executeBatch();
            //preparedStatement.close();
            //conn.close();
            //System.out.println("insert into db");

            //登录获取ticket
            String urlString= "http://localhost:8081/login/do_login";
            File file=new File("C:\\Users\\31502\\Desktop\\config.txt");
            if (file.exists()){
                file.delete();
            }
            RandomAccessFile raf= new RandomAccessFile(file,"rw");
            raf.seek(0);
            for (int i = 0; i < users.size(); i++) {
                User user = users.get(i);
                URL url=new URL(urlString);
                HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                httpURLConnection.setRequestMethod("POST");
                httpURLConnection.setDoOutput(true);
                OutputStream  outputStream= httpURLConnection.getOutputStream();
                String  params="mobile="+user.getId()+"&password="+MD5Util.inputPassToFormPass("123456");
                outputStream.write(params.getBytes());
                outputStream.flush();
                InputStream inputStream = httpURLConnection.getInputStream();
                ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                byte[] buff=new byte[1024];
                int len=0;
                while ((len=inputStream.read(buff))>=0){
                    byteArrayOutputStream.write(buff,0,len);
                }
                inputStream.close();
                byteArrayOutputStream.close();
                String response = new String(byteArrayOutputStream.toByteArray());


                ObjectMapper mapper = new ObjectMapper();
                RespBean respBean = mapper.readValue(response, RespBean.class);
                String ticket= (String) respBean.getObj();
                //System.out.println("create ticket"+ticket);
                //JSONObject jo = JSON.parseObject(response);
                //String token = jo.getString("data");
                //System.out.println("create token : " + user.getId());


                String row=user.getId()+","+ticket;
                raf.seek(raf.length());
                raf.write(row.getBytes());
                raf.write("\r\n".getBytes());
                //System.out.println("write to file:"+user.getId());

            }
            raf.close();
            System.out.println("结束");
        }


    private static Connection getConn() throws Exception {
        String url="jdbc:mysql://127.0.0.1:3306/seckill?characterEncoding=UTF-8&useUnicode=true&useSSL=false&tinyInt1isBit=false&allowPublicKeyRetrieval=true&serverTimezone=Asia/Shanghai";
        String username="root";
        String password="root";
        String driver="com.mysql.cj.jdbc.Driver";
        Class.forName(driver);
        return DriverManager.getConnection(url,username,password);
    }

    public static void main(String[] args) throws Exception {
        createUser(5000);
    }



}
